package com.example.Project.server;

import com.example.Project.Class.Apply;
import com.example.Project.Class.MassOrganizations;
import com.example.Project.Class.User;
import com.example.Project.Class.queryMass;
import com.example.Project.JDBC.JDBCMethods;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;

@RestController
@RequestMapping({"/api"})
public class HttpMethods {
    JDBCMethods jbdc = new JDBCMethods();

    public HttpMethods() throws ClassNotFoundException {
    }
    @GetMapping(value = "/getMassOrganizations")
    public ObservableList<MassOrganizations> getMassOrganizations(@RequestParam int page){
        try {
            System.out.println(page);
            return this.jbdc.getMassOrganizations(page);
        } catch (SQLException e) {
            e.printStackTrace();
            return FXCollections.observableArrayList(); // Use FXCollections to create an ObservableList
        }
    }
    @PostMapping(value="/deleteMassOrganization")
    public boolean deleteMassOrganization(@RequestParam int id){
        boolean flag=false;
        try {
            this.jbdc.deleteMassOrganization(id);
            flag=true;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return flag;
    }
    @PostMapping(value="/modifyMassOrganization")
    public boolean modifyMassOrganization(@RequestBody MassOrganizations mass){
        boolean flag=false;
        try{
            System.out.println(mass);
            this.jbdc.modifyMassOrganization(mass);
            flag=true;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return flag;
    }
    @GetMapping(value = "/getTotalPage")
    public int getTotalPage() throws SQLException {
        return this.jbdc.getTotalPage();
    }
    @PostMapping(value = "/getQueryMass")
    public ObservableList<MassOrganizations> getQueryMass(@RequestBody queryMass query){
        try {
            System.out.println(query.getId());
            System.out.println(query.getName());
            return this.jbdc.queryMassOrganization(query);
        } catch (SQLException e) {
            return FXCollections.observableArrayList();
        }
    }
    @PostMapping(value="/addMassOrganization")
    public boolean addMassOrganization(@RequestBody MassOrganizations mass){
        boolean flag=false;
        try{
            System.out.println(mass);
            flag= jbdc.addMassOrganization(mass);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return flag;
    }
    @GetMapping(value="/login")
    public boolean login(@RequestParam String identity,@RequestParam String account, @RequestParam String password){
        boolean flag=false;
        System.out.println(identity+account+password);
        flag=jbdc.login(identity,account,password);
        return flag;
    }
    @GetMapping(value="/getUserInfo")
    public User getUserInfo(@RequestParam int id) throws SQLException {
        return jbdc.getUserInfoById(id);
    }
    @GetMapping(value="/getUserMass")
    public ObservableList<MassOrganizations> getUserMass(@RequestParam int id) throws SQLException {
        return jbdc.getUserMassOrganizations(id);
    }
    @GetMapping(value="/getMassUser")
    public ObservableList<User> getMassUser(@RequestParam int id) throws SQLException {
        return jbdc.getMassUser(id);
    }
    @GetMapping(value= "/userSendAdd")
    public boolean userSendAdd(@RequestParam int userid,@RequestParam int massid) throws SQLException {
        System.out.println(userid+massid);
        return jbdc.userSendAdd(userid,massid);
    }
    @GetMapping(value= "/userSendDel")
    public boolean userSendDel(@RequestParam int userid,@RequestParam int massid,@RequestParam int i) throws SQLException {
        return jbdc.userSendDel(userid,massid,i);
    }
    @GetMapping(value="/getMasterMass")
    public ObservableList<MassOrganizations> getMasterMass(@RequestParam int id) throws SQLException {
        System.out.println("getMasterMass");
        return jbdc.getMasterMass(id);
    }
    @GetMapping(value="/getDelApply")
    public ObservableList<Apply> getDelApply(@RequestParam int id) throws SQLException {
        return jbdc.getDelApply(id);
    }
    @GetMapping(value="/getAddApply")
    public ObservableList<Apply> getAddApply(@RequestParam int id) throws SQLException {
        return jbdc.getAddApply(id);
    }
    @GetMapping(value= "/deleteMassUser")
    public boolean deleteMassUser(@RequestParam int userid,@RequestParam int massid) throws SQLException {
        return jbdc.deleteMassUser(userid,massid);
    }
    @GetMapping(value="/register")
    public int register(@RequestParam String name,@RequestParam String pass,@RequestParam String sex,@RequestParam String phone) throws SQLException {
        return jbdc.register(name,pass,sex,phone);
    }
}